package com.code.translate.plugins;

import com.code.translate.function.TextTranslatePlugin;
import com.code.translate.utils.CookieUtils;
import com.code.translate.utils.HttpUtils;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.Cookie;
import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ggh
 * @date 2022/8/5
 * <p>
 * papago翻译
 */
public class PapagoTranslatePlugin extends TextTranslatePlugin {
    private final static String indexUrl = "https://papago.naver.com/";
    private final static String translateUrl = "https://papago.naver.com/apis/n2mt/translate";
    private final static String refer = "https://papago.naver.com/";

    private final static Integer signleTranslateTextLen = 5000;

    private final static PapagoTranslatePlugin singleton = new PapagoTranslatePlugin();

    private PapagoTranslatePlugin() {
    }

    public static PapagoTranslatePlugin getSingleton() {
        return singleton;
    }


    @Override
    protected Map<String, Object> prepare() throws IOException {
        RequestEntity requestEntity = RequestEntity.get(URI.create(indexUrl)).build();
        ResponseEntity responseEntity = HttpUtils.reqEntity(requestEntity, false);

        Map<String, Cookie> cookieMap = CookieUtils.parseResponseCookie(responseEntity.getHeaders().get("set-cookie"));
        String cookie = CookieUtils.buildCookie(cookieMap.get("JSESSIONID"));


        Map<String, Object> params = new HashMap<>(1);
        params.put("cookie", cookie);
        return params;
    }

    @Override
    protected String doTranslate(String text, Map<String, Object> preParams) throws IOException {
        Map<String, String> headers = structHeaders(preParams);
        Map<String, Object> params = structCommonParams(preParams, text);


//        if (text.length() > signleTranslateTextLen) {
//            List<String> singleTextList = StringUtils.splitString(text, signleTranslateTextLen, Constants.SEGEMENT);
//
//            StringBuffer buffer = new StringBuffer();
//
//            for (String singleText : singleTextList) {
//                Object jsResult = JSUtils.execute("js/baidu-java.js", "tl", singleText);
//                if (jsResult != null) {
//                    params.put("sign", jsResult.toString());
//                }
//                params.put("query", singleText);
//
//                String result = HttpUtils.req(translateUrl, HttpMethod.POST, headers, params);
//
//                if (!StringUtils.isEmpty(result) && result.contains("status\":0,")) {
//                    buffer.append(JsonpathUtils.read(result, "$.trans_result.data[*].dst"));
//                }
//            }
//            return buffer.toString();
//        } else {
        params.put("query", text);
        String result = HttpUtils.req(translateUrl, HttpMethod.POST, headers, params);
        System.out.println(result);

//        if (!StringUtils.isEmpty(result) && result.contains("status\":0,")) {
//            return JsonpathUtils.read(result, "$.trans_result.data[*].dst");
//        }
//        }

        return null;
    }

    private static Map<String, Object> structCommonParams(Map<String, Object> preParams, String text) {
        Map<String, Object> params = new HashMap<>(3);
        params.put("source", "en");
        params.put("target", "zh-CN");
        params.put("locale", "zh-CN");
        params.put("dict", "true");
        params.put("dictDisplay", "30");
        params.put("honorific", "false");
        params.put("instant", "false");
        params.put("paging", "false");
        params.put("deviceId", "----");

        return params;
    }

    private static Map<String, String> structHeaders(Map<String, Object> preParams) {
        Map<String, String> headers = new HashMap<>(3);
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        headers.put("Referer", refer);
        headers.put("Cookie", preParams.get("cookie").toString());
        headers.put("device-type", "pc");
        headers.put("timestamp", String.valueOf(System.currentTimeMillis()));

        return headers;
    }
}
